﻿Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Public Class Edittype

    Dim objConn As OracleConnection
    Dim dtadapter As OracleDataAdapter
    Dim objCmd As New OracleCommand
    Dim da As OracleDataAdapter
    Dim cb As OracleCommandBuilder
    Dim ds As DataSet
    Dim Udone As New Err
    Dim Done As New Done
    Dim infor As New infor

    Private Sub BT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT1.Click
        Dim Edata As New Editdata
        Me.Close()
        Edata.Enabled = True
    End Sub

    Private Sub BT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT2.Click
        If textbox1.Text = "" Or TextBox2.Text = "" Then
            infor.ShowDialog()
        Else
            Editlbl.Visible = True
            editno.Visible = True
            editok.Visible = True
            BT1.Enabled = False
            BT2.Enabled = False
            BT3.Enabled = False
            Button1.Enabled = False
            textbox1.Enabled = False
            TextBox2.Enabled = False
        End If
    End Sub

    Private Sub BT3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT3.Click
        If textbox1.Text = "" Or TextBox2.Text = "" Then
            infor.ShowDialog()
        Else
            Dellbl.Visible = True
            Delno.Visible = True
            DelOk.Visible = True
            BT1.Enabled = False
            BT2.Enabled = False
            BT3.Enabled = False
            Button1.Enabled = False
            textbox1.Enabled = False
            TextBox2.Enabled = False
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text = "" Then
            infor.ShowDialog()
        Else
            TextBox2.Text = ""
            TextBox2.Enabled = True
            Try
                Dim sql As String = "select * from TYPE where TYPE_ID = '" + textbox1.Text + "'"
                dtadapter = New OracleDataAdapter(sql, objConn)
                Dim dt As New DataTable
                dtadapter.Fill(dt)

                TextBox2.Text = dt.Rows(0)("TYPE_MED")
            Catch ex As Exception
                Udone.ShowDialog()
                TextBox2.Enabled = False
            End Try
        End If

    End Sub

    Private Sub Edittype_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        editno.Visible = False
        editok.Visible = False
        Editlbl.Visible = False
        TextBox2.Enabled = False
        DataSC()
    End Sub

    Private Sub Delno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delno.Click
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        Button1.Enabled = True
        BTTrue()
        TextBox1.Enabled = True
        TextBox2.Text = ""
    End Sub

    Private Sub DelOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelOk.Click
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        Button1.Enabled = True
        BTTrue()
        TextBox1.Enabled = True

        Try
            Dim sql As String = "DELETE FROM TYPE WHERE TYPE_ID = '" + TextBox1.Text + "'"
            objCmd = New OracleCommand(sql, objConn)
            Try
                objCmd.ExecuteNonQuery()
                Done.ShowDialog()
            Catch ex As Exception
                Udone.ShowDialog()
            End Try
        Catch ex As Exception
            Udone.ShowDialog()
        Finally
            objConn.Dispose()
            textbox1.Text = ""
            TextBox2.Text = ""
            DataSC()
        End Try        
    End Sub

    Private Sub editok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editok.Click
        Editlbl.Visible = False
        editno.Visible = False
        editok.Visible = False
        BTTrue()
        Button1.Enabled = True
        TextBox1.Enabled = True

        Try
            Dim sql As String = "Update TYPE SET TYPE_ID = '" + TextBox1.Text + "',TYPE_MED = '" + TextBox2.Text + "' WHERE TYPE_ID = '" + TextBox1.Text + "'"
            objCmd = New OracleCommand
            With objCmd
                .Connection = objConn
                .CommandText = sql
                .CommandType = CommandType.Text
            End With
            Try
                objCmd.ExecuteNonQuery()
                Done.ShowDialog()
            Catch ex As Exception
                Udone.ShowDialog()
            End Try
        Catch ex As Exception
            Udone.ShowDialog()
        Finally
            objConn.Dispose()
            textbox1.Text = ""
            TextBox2.Text = ""
            DataSC()
        End Try       
    End Sub

    Private Sub editno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editno.Click
        Editlbl.Visible = False
        editno.Visible = False
        editok.Visible = False
        BTTrue()
        Button1.Enabled = True
        TextBox1.Enabled = True
        TextBox2.Text = ""
    End Sub

    Private Sub DataSC()
        Dim strConnString As String
        strConnString = Login.strConn
        objConn = New OracleConnection(strConnString)
        objConn.Open()
        Dim cmd2 As New OracleCommand
        Dim sql1 As String = "select * from TYPE ORDER BY TYPE_ID ASC"
        cmd2 = New OracleCommand(sql1, objConn)
        da = New OracleDataAdapter(cmd2)
        cb = New OracleCommandBuilder(da)
        ds = New DataSet()
        da.Fill(ds, "TYPE")

        With Me.textbox1
            .DataSource = ds.Tables("TYPE")
            .DisplayMember = "TYPE_ID"
            .ValueMember = "TYPE_ID"
        End With
    End Sub

    Private Sub BTTrue()
        BT1.Enabled = True
        BT2.Enabled = True
        BT3.Enabled = True
    End Sub

End Class